package com.song.mvc;

import com.song.mvc.config.MvcAppConfig;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import java.io.File;

/**
 * @Description: SpringApplicationSong：类描述
 * @author: songzhangyong
 * @Date: 2019/11/26 18:01
 */
public class SpringApplicationSong {


	public static void run(){

		try {
			AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();
			ac.register(MvcAppConfig.class);
			ac.refresh();


			File base = new File(System.getProperty("java.io.tmpdir"));

			Tomcat tomcat = new Tomcat();
			tomcat.setPort(8080);

			Context context = tomcat.addContext("/" ,base.getAbsolutePath()) ;

			DispatcherServlet dispatcherServlet = new DispatcherServlet(ac) ;
			Tomcat.addServlet(context , "song" , dispatcherServlet) ;

			context.addServletMapping("/" ,"song");
			tomcat.start();
			tomcat.getServer().await();
		} catch (Exception e) {
			e.printStackTrace();
		}


	}



}
